
// 根据child的ID 寻找祖先ID
export const childIdGeTAncestorsId = (arr, id = null) => {
  let route = arr.find((i) => i.meta.id == id);
  let routeMeta = route?.meta;
  if (routeMeta) {
    let { pid } = routeMeta;
    if (pid) {
      return childIdGeTAncestorsId(arr, pid);
    } else {
      return route;
    }
  }
};

// 逐级寻找路由
export const GraduallyRouter = (arr = [], id = null, glist = []) => {
  let route = arr.find((i) => i.meta.id == id);
  if (route) {
    let routeMeta = route?.meta;
    if (routeMeta) {
      let { pid } = routeMeta;
      if (pid) {
        let ParentInfo = arr.find((i) => i.meta.id == pid);
        let ParentMeat = ParentInfo.meta;
        if (ParentMeat.redirect.redirect_url == route.meta.path) {
          console.log("登录");
        } else {
          glist.unshift(route);
        }
        return GraduallyRouter(arr, pid, glist);
      } else {
        glist.unshift(route);
        return {
          GraduallyList: glist,
          GraduallyListNumber: glist.length,
          Ancestors: route,
        };
      }
    }
  }
};

// 格式化数据
export const FormatTreeList = (ArrayList) => {
  const ComponentPath = (Path) => () => import("../pages/" + Path + ".vue");
  ArrayList.forEach((item) => {
    let Meta = item.meta;
    let type = Meta.type ?? 0;
    let component = item.component;
    if (component) {
      if (type == 1) {
        if (component) {
          let page = component
          item.component = ComponentPath(page)
        } else {
          let page = `layout/components/router-view/router-view`;
          item.component = ComponentPath(page)
        }
      } else if (type == 0) {
        let page = component
        item.component = ComponentPath(page)
      }
    }
    item.children = FormatTreeList(item.children);
    return item;
  })
  return ArrayList;
};
export default {
  GraduallyRouter,
  childIdGeTAncestorsId,
  FormatTreeList,
};
